package com.jakewu.horizontalgridview.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.BaseCardView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewu.horizontalgridview.R;

public class EntryItemView extends BaseCardView {


    private ImageView mImageView;
    private View mInfoArea;
    private TextView mTitleView;
    private TextView mContentView;
    private ImageView mBadgeImage;
    //    private ImageView mBadgeFadeMask;
    private ImageView mContentImage;
    private boolean mbHasExtraDescription = false;
    private int mCardWidth = 0;
    private int mTextMargin = 0;

    public EntryItemView(Context context) {
        this(context,null,0);
    }


    public void setMainImage(Drawable drawable) {
        setMainImage(drawable, true);
    }


    public EntryItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.entry_item, this);

        mImageView = (ImageView) v.findViewById(R.id.main_image);
        mImageView.setVisibility(View.INVISIBLE);
        mInfoArea = v.findViewById(R.id.info_field);
        mTitleView = (TextView) v.findViewById(R.id.title_text);
        mContentView = (TextView) v.findViewById(R.id.content_text);
        mBadgeImage = (ImageView) v.findViewById(R.id.extra_badge);
//        mBadgeFadeMask = (ImageView) v.findViewById(R.id.fade_mask);
        mContentImage = (ImageView) v.findViewById(R.id.content_icon);

        if (mInfoArea != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbImageCardView,
                    defStyle, 0);
            try {
                setInfoAreaBackground(
                        a.getDrawable(R.styleable.lbImageCardView_infoAreaBackground));
            } finally {
                a.recycle();
            }
        }
    }

    /**
     * Set drawable with optional fade-in animation.
     */
    public void setMainImage(Drawable drawable, boolean fade) {
        if (mImageView == null) {
            return;
        }

        mImageView.setImageDrawable(drawable);
        if (drawable == null) {
            mImageView.setBackgroundColor(Color.TRANSPARENT);
            mImageView.animate().cancel();
            mImageView.setAlpha(1f);
            mImageView.setVisibility(View.INVISIBLE);
        } else {
            mImageView.setBackgroundColor(Color.BLACK);
            mImageView.setVisibility(View.VISIBLE);
            if (fade) {
                fadeIn(mImageView);
            } else {
                mImageView.animate().cancel();
                mImageView.setAlpha(1f);
            }
        }
    }

    public void setMainImageDimensions(int width, int height) {
        mCardWidth = width;
        ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
        lp.width = width;
        lp.height = height;
        mImageView.setLayoutParams(lp);
    }

    public void setInfoAreaBackground(Drawable drawable) {
        if (mInfoArea != null) {
            mInfoArea.setBackground(drawable);
//            if (mBadgeImage != null) {
//                mBadgeImage.setBackground(drawable);
//            }
        }
    }

    private void fadeIn(View v) {
        v.setAlpha(0f);
        v.animate().alpha(1f).setDuration(v.getContext().getResources().getInteger(
                android.R.integer.config_shortAnimTime)).start();
    }


    public void setTitleText(CharSequence text) {
        if (mTitleView == null) {
            return;
        }
        Paint paint = mTitleView.getPaint();
        float measureTextWidth = paint.measureText(text.toString());
        if (measureTextWidth < mCardWidth - mTextMargin) {
            mTitleView.setText(text);
        } else {
            mTitleView.setText(text+" ");//ellipsize:end
        }
//        setTextMaxLines();
    }


    public void setContentText(CharSequence text) {
        if (mContentView == null) {
            return;
        }

        mContentView.setText(text + " ");//ellipsize:end
        setTextMaxLines();
    }

    private void setTextMaxLines() {
        if (!mbHasExtraDescription) {
            Paint paint = mTitleView.getPaint();
            float measureTextWidth = paint.measureText(mTitleView.getText().toString());
            if (measureTextWidth < mCardWidth - mTextMargin) {
                mContentView.setMaxLines(2);
            } else {
                mContentView.setMaxLines(1);
            }
        }
//        if (TextUtils.isEmpty(getTitleText())) {
//            mContentView.setMaxLines(2);
//        } else {
//            mContentView.setMaxLines(1);
//        }
//        if (TextUtils.isEmpty(getContentText())) {
//            mTitleView.setMaxLines(2);
//        } else {
//            mTitleView.setMaxLines(1);
//        }
    }


}
